//
//  StudentActicitesDetailViewController.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/21.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "StudentActicitesDetailViewController.h"


@interface StudentActicitesDetailViewController () <UITableViewDataSource,UITableViewDelegate>


#pragma mark -- 属性 --
#pragma mark --
@property (weak, nonatomic) IBOutlet UIImageView *imagev;

@property (weak, nonatomic) IBOutlet UILabel *titlename;

@property (weak, nonatomic) IBOutlet UILabel *actor;

@property (weak, nonatomic) IBOutlet UILabel *starttime;

@property (weak, nonatomic) IBOutlet UILabel *endtime;

@property (weak, nonatomic) IBOutlet UILabel *adress;

@property (weak, nonatomic) IBOutlet UITextView *forecast;//简介

@property (weak, nonatomic) IBOutlet UIButton *applicat;//报名

@property (weak, nonatomic) IBOutlet UILabel *numberOfPerson;//人数


@property (weak, nonatomic) IBOutlet UIImageView *isoutImage;
@property (nonatomic,assign) BOOL isoutdate;//活动是否过期

@property (nonatomic,strong) NSString *aid;//活动id

@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property (nonatomic,assign) NSInteger pageNum;
@property (nonatomic,assign) NSInteger allpage;

@property (nonatomic,strong) NSMutableArray *arrSource;



@end

@implementation StudentActicitesDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableview.rowHeight = 50;
    [self dataShow];//数据
    
    
    [self addRefreshHeader];//表格视图
    [self addRefreshFooter];
    [self dataload];
    
    
    
    
}
#pragma mark -- dataShow --
#pragma mark --
- (void)dataShow{
    

    NSString *url =  BASEURLAPPEND(_detailSource[@"pic"]);
    [NSURL URLWithString:url];
    [_imagev sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"user_default"]];
    _titlename.text = _detailSource[@"name"];
    _actor.text =  [NSString stringWithFormat:@"%@%@",@"发起者:",_detailSource[@"teacherName"]];
    _starttime.text = [NSString stringWithFormat:@"%@%@",@"活动时间:",[NSDate dateWithTimeIntervalSince1970:[_detailSource[@"startTime"][@"time"] doubleValue]/1000]];
    _endtime.text = [NSString stringWithFormat:@"%@%@",@"报名截止时间:",[NSDate dateWithTimeIntervalSince1970:[_detailSource[@"catoffday"][@"time"] doubleValue]/1000]];
    
    _adress.text = [NSString stringWithFormat:@"%@%@",@"活动地点:",_detailSource[@"place"]];
    _forecast.text = [NSString stringWithFormat:@"%@%@",@"简介:",_detailSource[@"desce"]];
 
//    NSInteger totalNum = [_detailSource[@"totalNum"] integerValue];
    NSInteger currentNum = [_detailSource[@"careentNum"] integerValue];
    _numberOfPerson.text = [NSString stringWithFormat:@"已有%ld人参加",currentNum];
    
    _aid = _detailSource[@"id"];
    
    if ([_detailSource[@"isoutdate"] isEqualToString:@"ing"]) {
        _isoutdate = YES;
        _isoutImage.hidden = YES;
    }
    if ([_detailSource[@"isoutdate"] isEqualToString:@"out"]) {
        _isoutdate = NO;
        _isoutImage.hidden = NO;
    }
}

#pragma mark -- dataLoading --
#pragma mark --
- (void)dataload{

    NSMutableDictionary * parameters = [NSMutableDictionary dictionaryWithObjects:@[_aid,@(_pageNum+1),@(10)] forKeys:@[@"aid",@"pageNum",@"pageSize"]];

    [[NetTool sharedTool] requestMethod:POST loadingMsg:@"加载中..." url:BASEURLAPPEND(STUDENTACTICITIESDETAIL_URL) parameters:parameters finish:^(id responseObject, NSError *error) {
        
        [self loadDataSuccess:responseObject];
        
        [MBProgressHUD showSuccess:@"网络请求成功"];
        //更新视图
        
        [self.tableview reloadData];
        
    }];
    
    
}


#pragma mark -- 刷新 --
#pragma mark --
- (void)addRefreshHeader {
    
    _tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //下拉刷新
        _pageNum = 0;
//        [_arrSource removeAllObjects];
//        [self dataload];//当前第一页
        [MBProgressHUD showSuccess:@"刷新成功"];
        [_tableview reloadData];
        [_tableview.mj_header endRefreshing];
        
        
    }];

}

- (void)addRefreshFooter {
    
    
    _tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 上拉加载更多
        _pageNum = 0;
//        [_arrSource removeAllObjects];
//        [self dataload];//当前第一页
        [MBProgressHUD showSuccess:@"加载成功"];
        [_tableview reloadData];
        [_tableview.mj_footer endRefreshing];

        
//        if ((_arrSource.count + 10 > _allpage) && ((_arrSource.count < _allpage))) {
//            NSLog(@"未完，且add数量不足一页");
////            _pageNum 不加
//             _pageNum -= 1;//中和楼下+1
//            
//        }
//        
//        if (_arrSource.count >= _allpage){
//            [MBProgressHUD showError:@"没有更多数据！"];
//            [_tableview.mj_footer endRefreshing];
//            return ;
//        }//页数不足时结束刷新
//        
//        _pageNum += 1;//后一页
//        [self dataload];
//        [self.tableview reloadData];
//        [_tableview.mj_footer endRefreshing];
    }];
    
}

#pragma mark -- tableview的数据 --
#pragma mark --
- (void)loadDataSuccess:(NSDictionary *)result{
    
    NSMutableArray *temp = [NSMutableArray array];
    for (NSDictionary *dic in _arrSource) {//加上之前存在的数据
        [temp addObject:dic];
    }
    for (NSDictionary *dic in result[@"data"]) {//加上新数据
        [temp addObject:dic];
    }
    
    _arrSource = temp;//全部数据
    
    _allpage = [result[@"pageCount"] integerValue];
    NSLog(@"%@",_aid);
 
}
 
#pragma mark -- UITableViewDataSource,UITableViewDelegate --
#pragma mark --

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
     NSLog(@"_arrSource.count~~~~~%lu",(unsigned long)_arrSource.count);
     return _arrSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    StudentActicitesDeatilTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StudentActicitesDeatilTableViewCell"];
    if (!cell) {
        cell = [[StudentActicitesDeatilTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StudentActicitesDeatilTableViewCell"];
    }

    
    cell.name.text = _arrSource[indexPath.row][@"username"];
    cell.work.text =  _arrSource[indexPath.row][@"company"];
    cell.workName.text =  _arrSource[indexPath.row][@"job"];
    cell.address.text =  _arrSource[indexPath.row][@"addr"];
    cell.phone.text = _arrSource[indexPath.row][@"phone"];
   
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UITableViewHeaderFooterView *headerV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerV"];
    
    if (!headerV) {
        headerV = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"headerV"];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, 40)];
        label.backgroundColor = [UIColor clearColor];
        label.text = @"姓名                               手机号                           公司名称                            职位                                  所在地址";
        label.textAlignment = 1;
        label.font = [UIFont systemFontOfSize:24];
        label.layer.borderWidth = 0.3;
        [headerV addSubview:label];
    }
    return headerV;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44.0;
}



#pragma mark -- backBtn --
#pragma mark --

- (IBAction)backBtn:(UIButton *)sender {
    
    [self animationz:self.view];
    
}
#pragma mark -- 返回动画 --
#pragma mark --
- (void)animationz:(UIView *)someoneview{
    
    
    [UIView animateWithDuration:1 animations:^{
        someoneview.center = CGPointMake(self.view.center.x * 3, self.view.center.y);
    } completion:^(BOOL finished) {
        
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
        NSLog(@"返回动画");
        
    }];
    
}

@end
